package com.fubowen.reader.extension

import androidx.annotation.FloatRange
import androidx.core.graphics.ColorUtils

fun changeColor(
    color: Int,
    @FloatRange(0.0, 360.0) seWen: Float? = null,
    @FloatRange(0.0, 1.0) baoHeDu: Float? = null,
    @FloatRange(0.0, 1.0) brightness: Float? = null,
    isRatio: Boolean = false
): Int {
    val hsl = FloatArray(3)
    ColorUtils.colorToHSL(color, hsl)
    if (seWen != null) if (isRatio) hsl[0] *= seWen else hsl[0] = seWen
    if (baoHeDu != null) if (isRatio) hsl[1] *= baoHeDu else hsl[1] = baoHeDu
    if (brightness != null) if (isRatio) hsl[2] *= brightness else hsl[2] = brightness
    hsl[0] = hsl[0].coerceIn(0f, 360f)
    hsl[1] = hsl[1].coerceIn(0f, 1f)
    hsl[2] = hsl[2].coerceIn(0f, 1f)
    return ColorUtils.HSLToColor(hsl)
}